!(function($){
	"use strict";
	
	var Page = function() {};

	Page.prototype.init = function() {
		setInterval(() => {
			this.refreshLadda();
		}, 3000);
		this.event();
		this.loadTable();
    };
    
    Page.prototype.loadTable = function(){
    	var $newsTable = $('#news-table');
    	$newsTable.footable();
    	$('#news-table-entries').change(function (e) {
    		e.preventDefault();
    		var pageSize = $(this).val();
    		$newsTable.data('page-size', pageSize);
    		$newsTable.trigger('footable_initialized');
    	});
    	
    	var $imageTable = $('#image-table');
    	$imageTable.footable();
    	$('#image-table-entries').change(function (e) {
    		e.preventDefault();
    		var pageSize = $(this).val();
    		$imageTable.data('page-size', pageSize);
    		$imageTable.trigger('footable_initialized');
    	});
    	
    	var $videoTable = $('#video-table');
    	$videoTable.footable();
    	$('#video-table-entries').change(function (e) {
    		e.preventDefault();
    		var pageSize = $(this).val();
    		$videoTable.data('page-size', pageSize);
    		$videoable.trigger('footable_initialized');
    	});
    	
    	var $voiceTable = $('#voice-table');
    	$voiceTable.footable();
    	$('#voice-table-entries').change(function (e) {
    		e.preventDefault();
    		var pageSize = $(this).val();
    		$voiceTable.data('page-size', pageSize);
    		$voiceTable.trigger('footable_initialized');
    	});
    	
    },
    
    /**
     * 绑定事件
     */
    Page.prototype.event = function(){
		$('button[data-url]').each(function(i, btn){
			$(btn).ladda();
			$(btn).click(function(){
				$(btn).ladda('start');
				$.ajax({
					type: 'post',
					url: contextPath + $(btn).attr('data-url'),
					dataType: 'json',
					success: function(result){
						$(btn).ladda('stop');
						if(result.success){
							$.Notification.notify('custom','top left', '通知', '同步请求成功，请稍后查看结果');
						}
					}
				});
			});
		});
	},
	
	/**
	 * 刷新同步按钮
	 */
	Page.prototype.refreshLadda = function(){
		$.ajax({
			type: 'post',
			url: contextPath + '/admin/mp/material/async/result',
			dataType: 'json',
			success: function(result){
				if(result.data.asyncMaterialNews){
					$('#btn-async-news').ladda('start');
				} else {
					$('#btn-async-news').ladda('stop');
				}
				if(result.data.asyncMaterialImage){
					$('#btn-async-image').ladda('start');
				} else {
					$('#btn-async-image').ladda('stop');
				}
				if(result.data.asyncMaterialVideo){
					$('#btn-async-video').ladda('start');
				} else {
					$('#btn-async-video').ladda('stop');
				}
				if(result.data.asyncMaterialVoice){
					$('#btn-async-voice').ladda('start');
				} else {
					$('#btn-async-voice').ladda('stop');
				}
			}
		});
	},
    // init
	$.Page = new Page, $.Page.Constructor = Page;
})(jQuery);

$(function(){
	$.Page.init();
});
